package com.juku2024.juku.viewmodel


import com.juku2024.juku.model.AlbumListItemModel
import com.wanban.download.WBDownloadUtils
import com.wanban.download.db.DatabaseManager
import com.wanban.download.db.DatabaseUtils
import com.wanban.download.db.DownloadEntity
import com.wanban.download.db.DownloadSQLiteHelper
import com.wanban.download.model.Status

class DownloadViewModel : BaseViewModel() {


    fun getDownloadingData(success: (t: List<DownloadEntity>) -> Unit, fail: () -> Unit) {
        success(WBDownloadUtils.getUnSuccessTasks())
    }

    fun getAlbumList(success: (t: List<AlbumListItemModel>) -> Unit, fail: () -> Unit) {
        doAsync({
            // 获取所有下载任务
            val selection = DownloadSQLiteHelper.Columns.STATE + "=?"
            val selectionArgs = arrayOf(Status.SUCCESS.toString())
            val allDownloads = DatabaseManager.getInstance()?.getTasks(selection, selectionArgs)
            allDownloads?.forEach { task ->
                task.episodeCount = 0
            }
            return@doAsync allDownloads
        }, { result ->
            val albumList = ArrayList<AlbumListItemModel>()
            result.forEach { entity ->
                val albumVideo = albumList.firstOrNull { it.videoId == entity.videoId }
                if (albumVideo != null) {
                    albumVideo.albumCount++
                    albumVideo.albumTotalLength += entity.contentLength
                    albumVideo.albums?.add(entity)
                } else {
                    val item = AlbumListItemModel()
                    item.videoId = entity.videoId
                    item.name = entity.videoName
                    item.pic = entity.coverUrl
                    item.albumCount = 1
                    item.albumTotalLength = entity.contentLength
                    item.albums = ArrayList()
                    item.albums?.add(entity)
                    albumList.add(item)
                }

//                for (album in albumList) {
//                    if (album.videoId == entity.videoId) {
//                        isAddInAlbum = true
//                        album.albumCount++
//                        album.albumTotalLength += entity.contentLength
//                        break
//                    }
//                }
            }
            success(albumList)
        }, fail)
    }

    fun getDownloadFinishEpisodeList(videoId: Long, success: (t: List<DownloadEntity>) -> Unit, fail: () -> Unit) {
        doAsync({
            val selection = DownloadSQLiteHelper.Columns.VIDEO_ID + "=? " +
                    "AND " +
                    DownloadSQLiteHelper.Columns.STATE + "=?"
            val downloads = DatabaseUtils.getTasks(selection, arrayOf(videoId.toString(), Status.SUCCESS.toString()))
//                val downloads = DownloadRoomUtils.getInstance().downloadDao().getTasksByVideoIdAndStatus(videoId, Status.SUCCESS)

            return@doAsync downloads
        },{ result ->
            success(result.sortedBy { it.episode })
        },fail)
    }


}